<?php
/**
 * Created by PhpStorm.
 * User: ZhangZhi
 * Date: 2018-12-18 16:40
 */

namespace App\Admin\Controllers\UserInfo;

use App\Admin\Extensions\Grid;
use App\Admin\Extensions\Traits\HasResourceActions;
use App\Biz\Model\Config\ConfigProfessional;
use App\Biz\Model\Config\ConfigSubject;
use App\Biz\Model\ICMEUserInfo;
use App\Core\BaseController;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Layout\Content;
use Zttp\Zttp;

/**
 * 继教用户信息表
 *
 * Class ICMEUserInfoController
 * @package App\Admin\Controllers
 */
class ICMEUserInfoController extends BaseController
{
    use HasResourceActions;

    const PAGE_HEADER = "继教用户信息表";
    const PAGE_DESC_INDEX  = "列表";
    const PAGE_DESC_CREATE = "新建";
    const PAGE_DESC_UPDATE = "编辑";

    const FIELDS = [
        "user_id" => "用户姓名",
        "icme_config_user_title_id" => "职称",
        "profession_number" => "执业医师号",
        "introduction" => "简介",
        "specialty" => "擅长",
        "status" => "状态",
        "is_approval_ok" => "是否认证成功",
        "created_at" => "创建时间",
    ];

    /**
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(self::PAGE_HEADER)
            ->description(self::PAGE_DESC_INDEX)
            ->body($this->grid());
    }

    /**
     * @param         $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(self::PAGE_HEADER)
            ->description(self::PAGE_DESC_UPDATE)
            ->body($this->form()->edit($id));
    }

    /**
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(self::PAGE_HEADER)
            ->description(self::PAGE_DESC_CREATE)
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ICMEUserInfo());


        $grid->model();

        $grid->user_id(self::FIELDS['user_id'])->display(function() {
            $response = Zttp::withHeaders(['Fancy' => 'Pants'])->get('http://tcmstudy.com/api/user');
            return $response->json()['name'];
        });

        $grid->icme_config_user_title_id(self::FIELDS['icme_config_user_title_id'])->display(function($value) {
            return ConfigProfessional::find($value)->value('name');
        });
        $grid->profession_number(self::FIELDS['profession_number']);
        $grid->introduction(self::FIELDS['introduction']);

        $grid->specialty(self::FIELDS['specialty']);

        $states = [
            'on' => ['text' => '启用', 'value' => 1, 'color' => 'primary'],
            'off' => ['text' => '禁用', 'value' => 0, 'color' => 'danger'],
        ];
        $grid->status(self::FIELDS['status'])->switch($states);

        $grid->created_at(self::FIELDS['created_at']);

        // 操作工具部分
        $grid->disableRowSelector();

        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('profession_number', self::FIELDS['profession_number']);
        });

        $grid->actions(function(Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });


        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ICMEUserInfo());

        $form->display('user_id', self::FIELDS['user_id']);

        $form->select('icme_config_user_title_id', self::FIELDS['icme_config_user_title_id'])->options(ConfigProfessional::all()->pluck('name', 'id'));

        $form->text('profession_number',  self::FIELDS['profession_number'])->rules('required');

        $form->textarea('introduction', self::FIELDS['introduction'])->rules('required');

        $form->text('specialty', self::FIELDS['specialty'])->rules('required');

        $form->text('introduction', self::FIELDS['introduction'])->rules('required');

        $states = [
            'on' => ['text' => '启用', 'value' => 1],
            'off' => ['text' => '禁用', 'value' => 0],
        ];
        $form->switch('status', self::FIELDS['status'])->states($states);
        return $form;
    }
}
